package com.yy.distributed.lock.util;

import java.lang.reflect.Method;

/**
 * Write your description here...
 *
 * @author dinghuan
 * @date 2018/7/23
 */
public class DLockUtil {

    private static final String DEFAULT_PREFIX = "dLock.";

    public static String getClassMethodLockName(Class targetClass, Method method) {

        return getClassMethodLockName(targetClass, method.getName());
    }

    public static String getClassMethodLockName(Class targetClass, String method) {
        Package aPackage = targetClass.getPackage();
        String className = "";
        if (aPackage != null) {
            String[] strings = aPackage.getName().split("\\.");
            for (int i = 0; i < strings.length; i++) {
                className += strings[i].charAt(0) + ".";
            }
        } else {
            className = "default.";
        }

        return className + targetClass.getSimpleName() + "." + method;
    }

    public static String getClassMethodLockNameWithDefaultPrefix(Class targetClass, String method) {
        return DEFAULT_PREFIX + getClassMethodLockName(targetClass, method);
    }
}
